package com.yun2.bms.controller.user;

import com.yun2.bms.constant.SystemConstant;
import com.yun2.bms.model.Category;
import com.yun2.bms.model.User;
import com.yun2.bms.service.IUserService;
import com.yun2.bms.service.impl.UserServiceImpl;
import com.yun2.bms.utils.StringUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.ResourceBundle;

/**
 * author: youli
 * qq: 27855162@qq.com
 * date: 2020/6/4
 */

public class AddUserController implements Initializable {

    private IUserService userService = new UserServiceImpl();

    private ObservableList<User> userData = FXCollections.observableArrayList();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.cbGender.getItems().add(0, "女");
        this.cbGender.getItems().add(1, "男");

        this.cbRole.getItems().add(0, "管理员");
        this.cbRole.getItems().add(1, "教师");
        this.cbRole.getItems().add(2, "学生");
    }

    @FXML
    private ComboBox<String> cbGender;

    @FXML
    private Button btnCancel;

    @FXML
    private Button btnOK;

    @FXML
    private TextField tfPhone;

    @FXML
    private TextField tfUserName;

    @FXML
    private TextField tfEmail;

    @FXML
    private TextField tfLoginName;

    @FXML
    private ComboBox<String> cbRole;

    @FXML
    void add(ActionEvent event) {
        String loginName = this.tfLoginName.getText();
        String userName = this.tfUserName.getText();
        String gender = this.cbGender.getSelectionModel().getSelectedIndex() + "";
        String role = this.cbRole.getSelectionModel().getSelectedIndex()  + "";
        String phone = this.tfPhone.getText();
        String email = this.tfEmail.getText();
        String info = "";
        if (StringUtils.isBlank(loginName)) {
            info = "用户账号不能为空！";
        } else if (userService.isLoginNameUnique(loginName)) {
            info = "用户账号已经存在，请更换！";
        } else if (StringUtils.isBlank(userName)) {
            info = "用户姓名不能为空！";
        } else if (this.cbGender.getSelectionModel().getSelectedIndex() < 0) {
            info = "请选择性别";
        } else if (this.cbRole.getSelectionModel().getSelectedIndex() < 0) {
            info = "请选择角色";
        } else if (StringUtils.isBlank(phone)) {
            info = "电话号码不能为空！";
        } else if (!StringUtils.isMobile(phone) && !StringUtils.isPhone(phone)) {
            info = "电话号码格式验证错误！";
        } else if (StringUtils.isBlank(email)) {
            info = "电子邮件不能为空！";
        } else if (!StringUtils.isEmail(email)) {
            info = "电子邮件格式错误！";
        } else {
            User user = new User();
            user.setLoginName(loginName);
            user.setUserName(userName);
            user.setGender(gender);
            user.setRole(role);
            if (role.equals(SystemConstant.Role.ROLE_STUDENT)) {
                user.setMaxLendNumber(SystemConstant.MAX_LEND_NUMBER_OF_STUDENT);
                user.setMaxLendDays(SystemConstant.MAX_LEND_DAYS_OF_STUDENT);
            } else {
                user.setMaxLendNumber(SystemConstant.MAX_LEND_NUMBER_OF_TEACHER);
                user.setMaxLendDays(SystemConstant.MAX_LEND_DAYS_OF_TEACHER);
            }
            user.setPhone(phone);
            user.setEmail(email);
            user.setPassword(SystemConstant.INIT_PASSWORD);
            user.setCreateDate(new Date());
            int id = userService.add(user);
            if (id > 0) {
                user.setId(id);
                user.setGenderCn(this.cbGender.getValue());
                user.setRoleCn(this.cbRole.getValue());
                this.userData.add(user);
                info = "添加用户成功！";
            } else {
                info = "添加用户失败！";
            }
            //关闭当前窗口
            closeWindow();
        }
        //显示提示信息
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示信息");
        alert.setHeaderText(info);
        alert.showAndWait();
    }

    @FXML
    void cancel(ActionEvent event) {
        closeWindow();
    }

    private void closeWindow() {
        Stage stage = (Stage) this.btnOK.getScene().getWindow();
        stage.close();
    }


    public ObservableList<User> getUserData() {
        return userData;
    }

    public void setUserData(ObservableList<User> userData) {
        this.userData = userData;
    }
}
